home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / mmalloc / mmap-sup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-15  |  4.0 KB  |  136 lines

  1. /* Support for an sbrk-like function that uses mmap.
  2.    Copyright 1992 Free Software Foundation, Inc.
  3.  
  4.    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #if defined(HAVE_MMAP)
  21.  
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include <sys/mman.h>
  25.  
  26. #ifndef SEEK_SET
  27. #define SEEK_SET 0
  28. #endif
  29.  
  30. #include "mmalloc.h"
  31.  
  32. /* Cache the pagesize for the current host machine.  Note that if the host
  33.    does not readily provide a getpagesize() function, we need to emulate it
  34.    elsewhere, not clutter up this file with lots of kluges to try to figure
  35.    it out. */
  36.  
  37. static size_t pagesize;
  38.  
  39. #define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + pagesize - 1) & \
  40.                     ~(pagesize - 1))
  41.  
  42. /*  Get core for the memory region specified by MDP, using SIZE as the
  43.     amount to either add to or subtract from the existing region.  Works
  44.     like sbrk(), but using mmap(). */
  45.  
  46. PTR
  47. __mmalloc_mmap_morecore (mdp, size)
  48.   struct mdesc *mdp;
  49.   int size;
  50. {
  51.   PTR result = NULL;
  52.   off_t foffset;    /* File offset at which new mapping will start */
  53.   size_t mapbytes;    /* Number of bytes to map */
  54.   caddr_t moveto;    /* Address where we wish to move "break value" to */
  55.   caddr_t mapto;    /* Address we actually mapped to */
  56.   char buf = 0;        /* Single byte to write to extend mapped file */
  57.  
  58.   if (pagesize == 0)
  59.     {
  60.       pagesize = getpagesize ();
  61.     }
  62.   if (size == 0)
  63.     {
  64.       /* Just return the current "break" value. */
  65.       result = mdp -> breakval;
  66.     }
  67.   else if (size < 0)
  68.     {
  69.       /* We are deallocating memory.  If the amount requested would cause
  70.      us to try to deallocate back past the base of the mmap'd region
  71.      then do nothing, and return NULL.  Otherwise, deallocate the
  72.      memory and return the old break value. */
  73.       if (mdp -> breakval + size >= mdp -> base)
  74.     {
  75.       result = (PTR) mdp -> breakval;
  76.       mdp -> breakval += size;
  77.       moveto = PAGE_ALIGN (mdp -> breakval);
  78.       munmap (moveto, (size_t) (mdp -> top - moveto));
  79.       mdp -> top = moveto;
  80.     }
  81.     }
  82.   else
  83.     {
  84.       /* We are allocating memory.  Make sure we have an open file
  85.      descriptor and then go on to get the memory. */
  86.       if (mdp -> fd < 0)
  87.     {
  88.       result = NULL;
  89.     }
  90.       else if (mdp -> breakval + size > mdp -> top)
  91.     {
  92.       /* The request would move us past the end of the currently
  93.          mapped memory, so map in enough more memory to satisfy
  94.          the request.  This means we also have to grow the mapped-to
  95.          file by an appropriate amount, since mmap cannot be used
  96.          to extend a file. */
  97.       moveto = PAGE_ALIGN (mdp -> breakval + size);
  98.       mapbytes = moveto - mdp -> top;
  99.       foffset = mdp -> top - mdp -> base;
  100.       /* FIXME:  Test results of lseek() and write() */
  101.       lseek (mdp -> fd, foffset + mapbytes - 1, SEEK_SET);
  102.       write (mdp -> fd, &buf, 1);
  103.       mapto = mmap (mdp -> top, mapbytes, PROT_READ | PROT_WRITE,
  104.             MAP_SHARED | MAP_FIXED, mdp -> fd, foffset);
  105.       if (mapto == mdp -> top)
  106.         {
  107.           mdp -> top = moveto;
  108.           result = (PTR) mdp -> breakval;
  109.           mdp -> breakval += size;
  110.         }
  111.     }
  112.       else
  113.     {
  114.       result = (PTR) mdp -> breakval;
  115.       mdp -> breakval += size;
  116.     }
  117.     }
  118.   return (result);
  119. }
  120.  
  121. PTR
  122. __mmalloc_remap_core (mdp)
  123.   struct mdesc *mdp;
  124. {
  125.   caddr_t base;
  126.  
  127.   /* FIXME:  Quick hack, needs error checking and other attention. */
  128.  
  129.   base = mmap (mdp -> base, mdp -> top - mdp -> base,
  130.            PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
  131.            mdp -> fd, 0);
  132.   return ((PTR) base);
  133. }
  134.  
  135. #endif    /* defined(HAVE_MMAP) */
  136.